package com.taotao.content.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.taotao.common.pojo.EasyUITreeNode;
import com.taotao.common.pojo.TaotaoResult;
import com.taotao.content.service.ContentCategoryService;
import com.taotao.mapper.TbContentCategoryMapper;
import com.taotao.pojo.TbContentCategory;
import com.taotao.pojo.TbContentCategoryExample;
import com.taotao.pojo.TbContentCategoryExample.Criteria;
@Service
@Transactional
public class ContentCategoryServiceImpl implements ContentCategoryService{

	@Autowired
	private TbContentCategoryMapper ContentCategoryMapper;
	
	// 查询content节点
	@Override
	public List<EasyUITreeNode> getContentCategroyTreeNode(Long parentId) {
		TbContentCategoryExample example = new TbContentCategoryExample();
		Criteria criteria = example.createCriteria();
		criteria.andParentIdEqualTo(parentId);
		List<TbContentCategory> list = ContentCategoryMapper.selectByExample(example);
		
		List<EasyUITreeNode> treeList = new ArrayList<EasyUITreeNode>();
		for (TbContentCategory contentCategory : list) {
			EasyUITreeNode node = new EasyUITreeNode();
			node.setId(contentCategory.getId());
			node.setText(contentCategory.getName());
			node.setState(contentCategory.getIsParent()?"closed":"open");
			
			treeList.add(node);
		}
		return treeList;
	}
	
	// 增加content节点
	@Override
	public TaotaoResult createNode(Long parentId, String nodeName) {
		TbContentCategory contentCategory = new TbContentCategory();
		Date date = new Date();
		contentCategory.setCreated(date);
		contentCategory.setIsParent(false);
		contentCategory.setName(nodeName);
		contentCategory.setParentId(parentId);
		contentCategory.setSortOrder(1);
		contentCategory.setStatus(1);
		contentCategory.setUpdated(date);
		
		ContentCategoryMapper.insert(contentCategory);
		
		TbContentCategory contentCategory2 = ContentCategoryMapper.selectByPrimaryKey(parentId);
		if(!contentCategory2.getIsParent()){
			contentCategory2.setIsParent(true);
			ContentCategoryMapper.updateByPrimaryKey(contentCategory2);
		}
		return TaotaoResult.ok(contentCategory);
	}

	@Override
	public TaotaoResult updatateNode(Long id, String name) {
		TbContentCategory contentCategory = ContentCategoryMapper.selectByPrimaryKey(id);
		contentCategory.setName(name);
		ContentCategoryMapper.updateByPrimaryKey(contentCategory);
		return TaotaoResult.ok();
	}

	// 删除content节点
	@Override
	public TaotaoResult deleteNode(Long id) {
		TbContentCategory contentCategory = ContentCategoryMapper.selectByPrimaryKey(id);
		if(contentCategory.getIsParent()){
			ContentCategoryMapper.deleteByPrimaryKey(id);
			return TaotaoResult.ok();
		}
		
		deleteChildNode(id);
		return TaotaoResult.ok();
	}

	private void deleteChildNode(Long id) {
		TbContentCategoryExample example = new TbContentCategoryExample();
		Criteria criteria = example.createCriteria();
		criteria.andParentIdEqualTo(id);
		List<TbContentCategory> list = ContentCategoryMapper.selectByExample(example);
		if(list == null){
			return;
		}
		for (TbContentCategory contentCategory : list) {
			if(contentCategory.getIsParent()){
				ContentCategoryMapper.deleteByPrimaryKey(contentCategory.getId());
			}else{
				deleteChildNode(contentCategory.getId());
			}
		}
		
	}

}
